<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController as Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Model\Config;
use Illuminate\Support\Facades\Redis; 
/**
 * @menu_cat 数据配置
 * @icon cog
 * @sort 0
 */
class ConfigController extends Controller
{	
	// 配置验证规则
    protected $news_valid = [
        'key' => 'required',
        // 'value' => 'required',
    ];
    protected $channel_valid = [
        'channel_no' => 'required',
        'channel_name' => 'required',
    ];

    public function __invoke(){
        return $this->index(func_get_args());
    }

    /**
     * @menu 配置列表
     * @author guopeipei
     */
    public function index(){ 
        $list = Config::where('status', '=', '1')->orderBy('id', 'desc');
        $this->_search($list, $search);
        $this->_order($list);
        $list = $list->paginate(50);
        return view('Admin.Config.index')->with([
            'list' => $list,
            '_search' => $search
        ]);
    }

    /**
     * @name 添加配置
     * @author guopeipei
     */
    public function add(){
        if(request()->isMethod('get')){
            return view('Admin.Config.add');
        }
        $this->validate(request(), $this->news_valid);
        $param = request()->only(['key','desc']);
        $param['create_time'] = date('Y-m-d H:i:s');
        $id = Config::insertGetId($param);
        // 写入log
        // admin_log(['msg'=>$this->admin['name'].'添加了配置[ '.$id.' ]：'.$param['key']]);

        return redirect('/Config');
    }

    /**
     * @name 编辑配置
     * @author guopeipei
     */
    public function edit($id){
        if(request()->isMethod('get')){
            $data = Config::find($id);
            if(!$data){ abort(404); }

            return view('Admin.Config.edit')->with('data', $data);
        }

        $this->validate(request(), $this->news_valid);
        $param = request()->only(['key','desc']);
        $param['edit_time'] = date('Y-m-d H:i:s');
        Config::whereKey($id)
            ->sharedLock()
            ->update($param);

        // 写入log
        // admin_log(['msg'=>$this->admin['name'].'编辑了配置[ '.$id.' ]：'.$param['key']]);
        return redirect('/Config');
    }

    /**
     * @name 删除配置
     * @author guopeipei
     */
    public function remove($id){
        if(request()->isMethod('get')){
            $param['status']=-1;
            $param['edit_time'] = date('Y-m-d H:i:s');
            Config::where('id', '=', $id)
                ->update($param);
            return redirect('/Config');
        }     
    }

   /**
     * @name 查看属性
     * @author guopeipei
     */
    public function index_subclass($id){
       $data = Config::find($id);
       // var_dump($data->value);exit;
       if($data->value==""){
            $list= array('1' => ''); 
       }else{
            $list = String2Array($data->value);
       }
        return view('Admin.Config.subclass')->with([
            'list' => $list,
            'id' => $id
        ]);
    }

    /**
     * @name 编辑属性
     * @author guopeipei
     */
    public function edit_subclass($id){
        if(request()->isMethod('post')){
            $data =$_POST;
            unset($data['_token']);
            $data = array_combine($data['key'],$data['val']);

            // 更新缓存
            $key = Config::whereKey($id)->value('key');
            cache(['config.id.'.$id => $data], 43200);
            cache(['config.key.'.$key => $data], 43200);

            $param['value']=Array2String($data);
            Config::whereKey($id)
                    ->sharedLock()
                    ->update($param);
        }
        // 写入log
        // app('log')->useDailyFiles(storage_path().'/logs/Admin/admin.log');
        // app('log')->info($this->admin['name'].'编辑了配置[ '.$id.' ]：'.$param['title']);
        return redirect('/Config/index_subclass/'.$id);
    }

    /**
     * #@menu 推广渠道
     * @author guopeipei
     */
    public function index_channel(){
        $list = DB::table('channel');
        $this->_order($list, ['channel_id'=>'desc']);
        $list = $list->paginate(10);
        return view('Admin.Config.channel')->with('list', $list);
    }

    /**
     * #@name 添加渠道
     * @author guopeipei
     */
    public function add_channel(){
        if(request()->isMethod('get')){
            return view('Admin.Config.add_channel');
        }
        $this->validate(request(), $this->channel_valid);
        $param = request()->only(['channel_name','channel_no']);
        $param['add_time'] = date('Y-m-d H:i:s');
        $id = DB::table('channel')->insert($param);
        // 写入log
        // admin_log(['msg'=>$this->admin['name'].'添加了渠道[ '.$param['channel_no'].' ]：'.$param['channel_name']]);
        return redirect('/Config/index_channel');
    }

    /**
     * #@name 编辑渠道
     * @author guopeipei
     */
    public function edit_channel($id){
        if(request()->isMethod('get')){
            $data = DB::table('channel')->where('channel_id',$id)->first();
            if(!$data){ abort(404); }
            return view('Admin.Config.edit_channel')->with('data', $data);
        }

        $this->validate(request(), $this->channel_valid);
        $param = request()->only(['channel_no','channel_name']);
        DB::table('channel')->where('channel_id',$id)->sharedLock()->update($param);
        // 写入log
        // admin_log(['msg'=>$this->admin['name'].'编辑了渠道[ '.$id.' ]：'.$param['channel_name']]);
        return redirect('/Config/index_channel');
    }

    /**
     * #@name 删除渠道
     * @author guopeipei
     */
    public function remove_channel($id){
        if(request()->isMethod('get')){
            DB::table('channel')->where('channel_id', '=', $id)->delete();
            // 写入log
            // admin_log(['msg'=>$this->admin['name'].'删除了渠道[ '.$id.' ]']);
            return redirect('/Config/index_channel');
        }
    }
}
